﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;

namespace AspCompiler
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            Application.Run(new Form1());
        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Exception ex = e.ExceptionObject as Exception;
            string message = (ex == null) ? e.ExceptionObject.ToString() : ex.Message;

            using (FileStream stream = File.Open("error_log.txt", FileMode.Append))
            {
                StreamWriter writer = new StreamWriter(stream);
                writer.WriteLine(DateTime.Now.ToShortDateString() + ": " + message);
                writer.Close();
            }
        }

        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            using (FileStream stream = File.Open("error_log.txt", FileMode.Append))
            {
                StreamWriter writer = new StreamWriter("error_log.txt");
                writer.WriteLine(DateTime.Now.ToShortDateString() + ": " + e.Exception.Message);
                writer.Close();
            }
        }
    }
}
